package com.gviiii.baiduai.ai;

import com.baidu.aip.face.AipFace;
import com.gviiii.baiduai.auth.AuthClient;
import com.gviiii.baiduai.config.ConfigRegistry;
import com.gviiii.baiduai.pojo.FileInfo;
import com.gviiii.baiduai.pojo.FsResultInfo;
import com.gviiii.baiduai.util.FileUtil;
import com.gviiii.baiduai.util.JsonUtil;
import org.json.JSONObject;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.util.HashMap;
import java.util.Set;

/**
 * 人脸
 */
public class AiFaceservice {

    private ConfigRegistry configRegistry;

    private AuthClient authClient;

    private String userId;

    private String group;

    public AiFaceservice() {

    }

    @SuppressWarnings("unchecked")
    public AiFaceservice(ConfigRegistry configRegistry, String userId, String group) {
        this.configRegistry = configRegistry;
        this.authClient = AuthClient.getAuthClient();

        this.userId = userId;
        this.group = group;

    }

    public AiFaceservice(String configLocation, String userId, String group) {
    }

    public AiFaceservice(String userId, String group) {
    }

    /**
     * @param client
     * @param fileInfo
     */
    public void faceSearch(AipFace client, FileInfo fileInfo, String userid, String group) {
        // 传入可选参数调用接口
        HashMap<String, String> options = new HashMap<>();
//        options.put("quality_control", "NORMAL");
//        options.put("liveness_control", "LOW");
        options.put("user_id", userid);
        options.put("max_user_num", "3");

        String image = FileUtil.encodeFiles(fileInfo.getFilePath());
        String imageType = "BASE64";
        String groupIdList = group;

        // 人脸搜索
        JSONObject res = client.search(image, imageType, groupIdList, options);
        if ("SUCCESS".equals(res.getString("error_msg"))) {
            FsResultInfo result = (FsResultInfo) JsonUtil.readObjectByString(res.get("result").toString(), FsResultInfo.class);
            if (result.getUserList().size() > 0) {
                result.getUserList().forEach(user -> {
                    if (user.getScore()>60){
                        String rootDir = Paths.get(FileUtil.getFileDir(fileInfo.getFilePath())).getParent().toString();
                        FileUtil.createDir(rootDir,user.getUserId());
                        try {
                            Files.copy(Paths.get(fileInfo.getFilePath()),Paths.get(rootDir,user.getUserId(),fileInfo.getFileName()+fileInfo.getFileExName()), StandardCopyOption.REPLACE_EXISTING);
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                        System.out.println("ID: " + result.getFaceToken() + " 图片: " + fileInfo.getFilePath() + " 用户: " + user.getUserId() + " 可信度: " + user.getScore());
                    }
                });
            }
        }
    }

    public void faceSearch(AipFace aipFace, Set<FileInfo> fileInfoSet, String userid, String group) {
        fileInfoSet.forEach(fileInfo -> {
            faceSearch(aipFace, fileInfo, userid, group);
        });
    }

    public void faceSearch(FileInfo fileInfo) {
        faceSearch(authClient.aipFaceClient(configRegistry), fileInfo, userId, group);
    }

    public void faceSearch(Set<FileInfo> fileInfoSet) {
        faceSearch(authClient.aipFaceClient(configRegistry), fileInfoSet, userId, group);
    }
}
